package com.google.code.lightsomy.output;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Lightsomy template renderer.
 * Default interface to render templates using various templating engines.
 * 
 * @author Viktoras Agejevas
 *
 */
public abstract class Renderer {

	/**
	 * Set a variable for the template.
	 * 
	 * @param key variable name
	 * @param value variable value
	 */
	public abstract void set(String key, Object value);

	/**
	 * Get template variable by name.
	 * 
	 * @param key variable name
	 * @return template variable
	 */
	public abstract Object get(String key);
	
	/**
	 * Render template by template file location (from webapp root).
	 * 
	 * @param page template location
	 * @return rendered representation of the template
	 */
	public abstract String render(String page);

	/**
	 * Render template using conventional Lightsomy template naming convention: 
	 * <code>/view/[controller]/[action].[ext]</code>
	 * 
	 * @return rendered representation of the template
	 */
	public abstract String render();

	/**
	 * Output rendered contents.
	 * 
	 * @param content HTML to output to response
	 */
	public abstract void output(String content);

	/**
	 * Master template file path.
	 * 
	 * @param page path to a master template file (from webapp root)
	 */
	public abstract void setLayout(String page);
	
	/**
	 * Get master template file path.
	 * 
	 * @return master template file path
	 */
	public abstract String getLayout();
	
	/**
	 * Template type.
	 * 
	 */
	public enum Type {
		JSP(".jsp"), 
		VELOCITY(".tpl"), 
		FREE_MARKER(".tpl");
		
		private String extension;
		
		Type(String ext) {
			this.extension = ext;
		}
		
		/**
		 * Returns template extension registered for this template type.
		 * @return template extension preceded with a dot, for example (.tpl)
		 */
		public String getExtension() {
			return extension;
		}
	}
	
	public static Renderer getRenderer(Type type, final HttpServletRequest request,
			final HttpServletResponse response) {
		switch (type) {
			case JSP:
				return new JspRenderer(request, response);
			case VELOCITY:
				return new VelocityRenderer(request, response);
			case FREE_MARKER:
				return new FreeMarkerRenderer(request, response);
		}

		return null;
	}
}
